package com.lagou.service.protocol;

import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * json序列化方式
 */
public class JSONSerializer implements Serializer {
    public byte[] serialize(Object object) throws IOException {
        String json = JSON.toJSONString(object);
        return json.getBytes(Charset.forName("UTF-8"));
    }

    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        String json = new String(bytes, Charset.forName("UTF-8"));
        return JSON.parseObject(json, clazz);
    }
}
